package cn.yanhu.kuwanapp.common.viewmodels

import androidx.lifecycle.MutableLiveData
import cn.woochen.comlib.BaseViewModel
import cn.woochen.comlib.util.ApplicationUtil
import cn.woochen.comlib.util.ConfigPref
import cn.woochen.comlib.util.PackageUtil
import cn.woochen.comlib.util.UserPref
import cn.yanhu.kuwanapp.bean.other.VersionBean
import cn.yanhu.kuwanapp.http.status.ResultState
import cn.yanhu.kuwanapp.utils.ChannelUtils
import cn.yanhu.kuwanapp.utils.DeviceInfoUtil
import cn.yanhu.kuwanapp.utils.constans.Constants
import cn.yanhu.kuwanapp.utils.ext.request
import cn.yanhu.kuwanapp.utils.ext.rxApi


class CommonViewModel: BaseViewModel() {
    private var mUserId by UserPref(Constants.USER_ID, "")
    private var mDeviceOaid by ConfigPref(Constants.DEVICE_OAID, "")
    val eventTraceInfo  = MutableLiveData<ResultState<Any>>()
    private var mImei by UserPref(Constants.IMEI, "")

    fun eventTrace(event_key:String,buttonName:String = ""){
        var imei = mDeviceOaid
        if (mDeviceOaid.isBlank()) imei = mImei
        val map = mutableMapOf<String, String>()
        map["app_market_channel"] = ChannelUtils.getChannel()
        map["package_name"] = ApplicationUtil.context.packageName
        map["app_version"] = PackageUtil.getCurrentVersionName(ApplicationUtil.context)
        map["customer_id"] = mUserId
        map["udid"] = imei
        map["button_name"] = buttonName
        map["event_key"] = event_key
        request({ rxApi.eventTrace(map) }, eventTraceInfo,true)
    }


    val checkUpdateData  = MutableLiveData<ResultState<VersionBean>>()

    /**
     * 检测更新
     */
    fun checkUpdate(){
        request({ rxApi.checkUpdate() }, checkUpdateData,true)
    }


}